/*******************************************************************************
 *  Copyright (c) 2017 Oliver Meili
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Eclipse Distribution License v1.0 which accompany this distribution.
 *   
 *  The Eclipse Public License is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *  The Eclipse Distribution License is available at
 *  http://www.eclipse.org/org/documents/edl-v10.php.
 *   
 *  Contributors:
 *  Oliver Meili <omi@ieee.org>
 *******************************************************************************/
package org.eclipse.vorto.codegen.ble.alpwise.templates

import java.io.File
import java.util.UUID
import org.eclipse.vorto.codegen.api.InvocationContext
import org.eclipse.vorto.codegen.ble.model.BleInvocationContext
import org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel
import org.eclipse.vorto.codegen.ble.templates.BleGattTemplate

class AlpwiseFbSourceTemplate extends BleGattTemplate<FunctionblockModel> {

	UUID uuid

	override getFileName(FunctionblockModel fb) {
		return fb.name + ".c";
	}

	override getPath(FunctionblockModel fb) {
		return rootPath + File.separator + fb.name;
	}

	override getContent(FunctionblockModel fb, InvocationContext context) {
		'''
/* «fb.name» generated by Vorto */

#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include <BleTypes.h>
#include <attserver.h>

#include "BleUtils.h"
#include "BleApp_Cbk.h"

#include "«fb.name».h"
#include "services/«findService((context as BleInvocationContext).getDevice(), fb).name»Service.h"

const «fb.name»_t «fb.name»_Instances[NUM_«fb.name.toUpperCase»_SERVICES] = {
	{
		&«findService((context as BleInvocationContext).getDevice(), fb).name»Service_Instances[0],
	
		«FOR status : fb.functionblock.status.properties»
			«IF context.getMappedElement(status, "source").hasAttribute("uuid")»
					&«findService((context as BleInvocationContext).getDevice(), fb).name»Service_Instances[0].«findCharacteristic((context as BleInvocationContext).getDevice(), status).name.toFirstUpper»Value,
			«ENDIF»
		«ENDFOR»
	}
};

«FOR status : fb.functionblock.status.properties»
	«IF context.getMappedElement(status, "source").hasAttribute("uuid")»
		void «fb.name»_Set«status.name.toFirstUpper»Value(«fb.name»_t *«fb.name.toLowerCase», «context.getMappedElement(status, "source").getAttributeValue("datatype", "uint16")»_t value)
		{
			/* Data conversion based on little endian byte order */
			for (int i = 0; i < «context.getMappedElement(status, "source").getAttributeValue("length", "2")»; i++)
			{
				«fb.name.toLowerCase»->«status.name.toFirstUpper»Value[i+«context.getMappedElement(status, "source").getAttributeValue("offset", "0")»] = (uint8_t) (value >> (8*i));
			}
		
			«findService((context as BleInvocationContext).getDevice(), fb).name»Service_WriteAndNotifyValue(&«fb.name.toLowerCase»->service->«findCharacteristic((context as BleInvocationContext).getDevice(), status).name.toFirstUpper», «fb.name.toLowerCase»->«status.name.toFirstUpper»Value, «findCharacteristic((context as BleInvocationContext).getDevice(), status).length», 1);
		}
	«ENDIF»
«ENDFOR»

'''
	}
}
